# include "MessageHandle.h"
# include "Client.h"
# include <qstring.h>
# include "App.h"
# include "VideoDecoder.h"
# include "ShowRemoteWindow.h"

MessageHandle::MessageHandle(App* _app)
{
	this->m_app = _app;
}

void MessageHandle::parse(char* _key, std::string _payload) {
	
	CustomHeader header = CustomHeader(std::atoi(_key));
	int a = 0;
	switch (header)
	{
	case UnKnown:
		break;
	case Message:
		this->output(QString::fromStdString(_payload));
		break;
	case VideoFrame:
	{
		this->m_app->onRemoteVideoFrame(reinterpret_cast<uint8_t*>(_payload.data()), _payload.size());
	}
		// this->output(QString::number(_payload.size()));
		break;
	case AudioFrame:
		break;
	case EncodeContext:
	{
		size_t offset = 0;
		VideoDecoder::parse(_payload.data(), offset);
		this->m_app->createDecodeBuffer(2560 * 1440 * 4);
		this->m_app->initVideoDecoder(reinterpret_cast<uint8_t*>(_payload.data()));
	}
	break;
	default:
		break;
	}
}

void MessageHandle::sendEncodeContext(Client* _client, int conv, uint8_t* _data, size_t _size)
{
	this->sendData(_client, conv, CustomHeader::EncodeContext, _data, _size);
}

void MessageHandle::sendVideoData(Client* _client, int conv, uint8_t* _data, size_t _size)
{
	this->sendData(_client, conv, CustomHeader::VideoFrame, _data, _size);
}

void MessageHandle::sendData(Client* _client, int conv, CustomHeader _header, uint8_t* _data, size_t _size)
{
	_client->sendMessage(conv, std::move(std::to_string(_header)), std::move(std::string(reinterpret_cast<char*>(_data), _size)));
}
